﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace HKTools
{
    /// <summary>
    /// 判断题模板类
    /// </summary>
    public class HK_TrueFalseItem : HK_QuesItem
    {
        public Toggle right_Toggle;
        public Toggle fault_Toggle;

        List<GameObject> optionBG_List = new List<GameObject>();

        List<Toggle> option_List = new List<Toggle>();

        int answerID;

        public void InitQuesItem(HK_TrueFalseData quesData, int id)
        {
            ID_Txt.text = id.ToString() + "、";

            answerID = quesData.AnswerID;

            option_List.Add(right_Toggle);
            option_List.Add(fault_Toggle);

            right_Toggle.isOn = false;
            fault_Toggle.isOn = false;

            quesName_Txt.text = quesData.QuesName;

            optionBG_List.Add(right_Toggle.transform.Find("Background").gameObject);
            optionBG_List.Add(fault_Toggle.transform.Find("Background").gameObject);

            right_Toggle.onValueChanged.AddListener(OnSelectRightToggle);
            fault_Toggle.onValueChanged.AddListener(OnSelectFaultToggle);

            analysis_Txt.text = quesData.Analysis;
        }

        void OnSelectRightToggle(bool isOn)
        {
            if (!isOn)
                return;

            isCorrect = answerID == 1 ? true : false;
        }

        void OnSelectFaultToggle(bool isOn)
        {
            if (!isOn)
                return;

            isCorrect = answerID == 0 ? true : false;
        }

        public void HideToggle()
        {
            for (int i = 0; i < optionBG_List.Count; i++)
            {
                optionBG_List[i].SetActive(false);
            }
        }

        public override bool IsDone()
        {
            isDone = false;

            for (int i = 0; i < option_List.Count; i++)
            {
                if (option_List[i].isOn)
                    isDone = true;
            }

            return isDone;
        }

        public override bool IsCorrect()
        {
            return isCorrect;
        }
    }
}